package com.henghai.zhenxuan.ui.page.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.R;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.domain.request.SendSmsRequester;
import com.henghai.zhenxuan.domain.request.UpdatePwdRequester;
import com.henghai.zhenxuan.utils.MainHandler;
import com.blankj.utilcode.util.ToastUtils;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseActivity;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

public class UpdatePwdActivity extends BaseActivity {
    private ForgotPwdStates forgotPwdStates;
    private UpdatePwdRequester mUpdatePwdRequester;
    private CountDownTimer countDownTimer;

    private SendSmsRequester mSendSmsRequester;

    @Override
    protected void initViewModel() {
        forgotPwdStates = getActivityScopeViewModel(ForgotPwdStates.class);
        mUpdatePwdRequester = getActivityScopeViewModel(UpdatePwdRequester.class);

        mSendSmsRequester = getActivityScopeViewModel(SendSmsRequester.class);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUpdatePwdRequester.getTokenResult().observe(this, dataResult -> {
            if (!dataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(dataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            TipDialog.show("修改成功", WaitDialog.TYPE.SUCCESS);
            MainHandler.INSTANCE.postDelay(2000, () -> {
                AppConfig.INSTANCE.getMConfigs().token().set("");
                startActivity(new Intent(UpdatePwdActivity.this, LoginActivity.class));
                UpdatePwdActivity.this.finish();
            });
        });
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_update_pwd, BR.vm, forgotPwdStates).addBindingParam(BR.click, new ClickProxy());
    }

    private void startCountDown() {
        if (countDownTimer != null) {
            countDownTimer.start();
        }
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                forgotPwdStates.codeText.set(millisUntilFinished / 1000 + "");
            }

            @Override
            public void onFinish() {
                forgotPwdStates.codeText.set("重新发送");
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        //销毁定时器
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.onDestroy();
    }

    public class ClickProxy {
        public void newPwdBrowse() {
            forgotPwdStates.newPwdBrowse.set(!forgotPwdStates.newPwdBrowse.get());
        }

        public void confirmPwdBrowse() {
            forgotPwdStates.confirmPwdBrowse.set(!forgotPwdStates.confirmPwdBrowse.get());
        }

        public void send() {
            if (forgotPwdStates.userPhone.get().isEmpty()) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
            if (forgotPwdStates.codeText.get().equals("重新发送") || forgotPwdStates.codeText.get().equals("发送验证码")) {
                mSendSmsRequester.request(UpdatePwdActivity.this, forgotPwdStates.userPhone.get());
                startCountDown();
            }
        }

        public void commit() {
            if (forgotPwdStates.userPhone.get().isEmpty()) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
//            if (forgotPwdStates.authCode.get().isEmpty()) {
//                ToastUtils.showShort("请输入验证码");
//                return;
//            }
            if (forgotPwdStates.oldPwd.get().isEmpty()) {
                ToastUtils.showShort("请输入旧密码");
                return;
            }
            if (forgotPwdStates.newPwd.get().isEmpty()) {
                ToastUtils.showShort("请输入新密码");
                return;
            }

            if (forgotPwdStates.newPwd.get().length() < 6) {
                ToastUtils.showShort("密码长度最少需要6位");
                return;
            }

            if (!forgotPwdStates.confirmPwd.get().equals(forgotPwdStates.newPwd.get())) {
                ToastUtils.showShort("密码输入不一致");
                return;
            }
            mUpdatePwdRequester.request(UpdatePwdActivity.this, forgotPwdStates.oldPwd.get(), forgotPwdStates.newPwd.get());
        }
    }

    public static class ForgotPwdStates extends StateHolder {
        public final State<Boolean> newPwdBrowse = new State<>(false);
        public final State<Boolean> confirmPwdBrowse = new State<>(false);
        public final State<String> codeText = new State<>("发送验证码");
        public final State<String> userPhone = new State<>(AppConfig.INSTANCE.getMConfigs().user().get().getPhonenumber());
        public final State<String> authCode = new State<>("");

        public final State<String> oldPwd = new State<>("");
        public final State<String> newPwd = new State<>("");
        public final State<String> confirmPwd = new State<>("");
    }
}
